﻿using CommonEntity.CommonImageEntities;
using ReactiveUI.Fody.Helpers;
using ReactiveUI;
using ScienceAIApp.ViewModels.SubViewModels;
using ScienceAppService.ModuleInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Text;
using System.Threading.Tasks;

namespace ScienceAIApp.ViewModels.AlertViewModel;
public class HistoryRecordViewModel : AlertBaseViewModel
{
    private IImageShowService _imageShowService;
    private IHistoryImageService _history;
    private IMarkDrawService _markDrawService;
    
    private IList<ImageRecordViewModel> _recordVms { get; set; }

    [Reactive] public ImageRecordViewModel RecordVm1 { get; set; }
    [Reactive] public ImageRecordViewModel RecordVm2 { get; set; }
    [Reactive] public ImageRecordViewModel RecordVm3 { get; set; }
    [Reactive] public ImageRecordViewModel RecordVm4 { get; set; }
    [Reactive] public ImageRecordViewModel RecordVm5 { get; set; }
    [Reactive] public ImageRecordViewModel RecordVm6 { get; set; }
    [Reactive] public ImageRecordViewModel RecordVm7 { get; set; }
    [Reactive] public ImageRecordViewModel RecordVm8 { get; set; }
    [Reactive] public ImageRecordViewModel RecordVm9 { get; set; }
    [Reactive] public ImageRecordViewModel RecordVm10 { get; set; }

    [Reactive] public int PageNum { get; set; }
    [Reactive] public int PageCount { get; set; }

    [Reactive] public bool IsCanPre { get; set; }
    [Reactive] public bool IsCanNext { get; set; }
    [Reactive] public string AlertMsg { get; set; }

    public ReactiveCommand<bool, Unit> TurnPageCommand { get; set; }
    public ReactiveCommand<Unit, Unit> CancelCommand { get; set; }
    public ReactiveCommand<Unit, Unit> ConfirmCommand { get; set; }

    public HistoryRecordViewModel(ImageRecordViewModel vm1, ImageRecordViewModel vm2,
        ImageRecordViewModel vm3, ImageRecordViewModel vm4, ImageRecordViewModel vm5,
        ImageRecordViewModel vm6, ImageRecordViewModel vm7, ImageRecordViewModel vm8,
        ImageRecordViewModel vm9, ImageRecordViewModel vm10,
        IImageShowService imageShowService, IHistoryImageService historyImageService,
        IMarkDrawService markDrawService)
    {
        _imageShowService = imageShowService;
        _markDrawService = markDrawService;
        _history = historyImageService;

        RecordVm1 = vm1;
        RecordVm2 = vm2;
        RecordVm3 = vm3;
        RecordVm4 = vm4;
        RecordVm5 = vm5;
        RecordVm6 = vm6;
        RecordVm7 = vm7;
        RecordVm8 = vm8;
        RecordVm9 = vm9;
        RecordVm10 = vm10;

        _recordVms = new List<ImageRecordViewModel>();
        _recordVms.Add(RecordVm1);
        _recordVms.Add(RecordVm2);
        _recordVms.Add(RecordVm3);
        _recordVms.Add(RecordVm4);
        _recordVms.Add(RecordVm5);
        _recordVms.Add(RecordVm6);
        _recordVms.Add(RecordVm7);
        _recordVms.Add(RecordVm8);
        _recordVms.Add(RecordVm9);
        _recordVms.Add(RecordVm10);

        PageNum = 0;
        PageCount = 0;

        foreach (var svm in _recordVms)
        {
            svm.ChooseRecordInteraction.RegisterHandler(ctx =>
            {
                ChooseRecordInteractionHandler(ctx.Input.Id);
                ctx.SetOutput(Unit.Default);
            });

            svm.DeleteRecordInteraction.RegisterHandler(async ctx =>
            {
                var records = await _history.GetImageRecords(PageNum, 10);
                FrushRecordVM(records);
                ctx.SetOutput(Unit.Default);
            });
        }

        TurnPageCommand = ReactiveCommand.CreateFromTask<bool>(async flag =>
        {
            if (PageCount == 0) return;
            // 上一页
            if (!flag)
            {
                if(PageNum == 1) return;
                PageNum -= 1;
                if (PageCount == 0)
                    PageNum = 0;
                else if(PageNum < 1)
                {
                    PageNum = 1;
                }
            }
            // 下一页
            else
            {
                if (PageNum == PageCount) return;
                PageNum += 1;
                if (PageNum > PageCount)
                    PageNum = PageCount;
            }
            var records = await _history.GetImageRecords(PageNum, 10);
            FrushRecordVM(records);
        });

        CancelCommand = ReactiveCommand.CreateFromTask(async () =>
        {
            await AppEnvironment.Instance.AlertWindow.Hide();
        });

        // 选中序列, 点击 confirm
        ConfirmCommand = ReactiveCommand.CreateFromTask(async () =>
        {
            var selectVm = _recordVms.FirstOrDefault(svm => svm.IsChecked);

            if (selectVm == null)
            {
                AlertMsg = "Please select the sequence first ";
                return;
            }

            await _imageShowService.SetNiiGzFile(selectVm.CurrentRecord!.Directory);
            _markDrawService!.ClearData();
            await _imageShowService.SetImageInfo();
            await _imageShowService.GetTagInfoMat();
            await AppEnvironment.Instance.AlertWindow.Hide();
        });

    }

    private void ChooseRecordInteractionHandler(int id)
    {
        RxApp.MainThreadScheduler.Schedule(() =>
        {
            AlertMsg = "";
            foreach (var vm in _recordVms)
            {
                if (vm.IsChecked)
                {
                    vm.IsChecked = false;
                }
                else
                {
                    vm.IsChecked = vm.CurrentRecord!.Id == id;
                }
            }

        });
    }

    public async Task InitialHistory()
    {
        var count = await _history.GetRecordCount();
        PageCount = count / 10 + 1;
        PageNum = 1;
        var records = await _history.GetImageRecords(1, 10);
        FrushRecordVM(records);
    }

    /// <summary>
    /// 刷新 record列表
    /// </summary>
    /// <param name="records"></param>
    public void FrushRecordVM(List<ImageRecord> records)
    {
        if (records?.Count == 0) return;
        RxApp.MainThreadScheduler.Schedule(() =>
        {
            for (var i = 0; i < records!.Count; i++)
            {
                _recordVms[i].SetRecord(records[i]);
            }

            for (var i = records.Count; i < 10; i++)
            {
                _recordVms[i].SetRecord(null);
            }

        });
    }

}
